package online.longzipeng.cinemasys.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import online.longzipeng.cinemasys.common.annotion.NeedPermissions;
import online.longzipeng.cinemasys.common.exception.AppException;
import online.longzipeng.cinemasys.common.result.ResponseEnum;
import online.longzipeng.cinemasys.common.result.Result;
import online.longzipeng.cinemasys.dto.movie.MovieQueryResponseDTO;
import online.longzipeng.cinemasys.entity.Movie;
import online.longzipeng.cinemasys.service.MovieService;
import online.longzipeng.cinemasys.vo.movie.MovieQueryVO;
import online.longzipeng.cinemasys.vo.movie.MovieSaveVO;
import online.longzipeng.cinemasys.vo.movie.MovieUpdateVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lzp
 * @since 2021-04-08
 */
@RestController
@RequestMapping("/movie")
@Api(value = "电影相关接口",description = "电影相关接口",tags = "电影相关接口")
public class MovieController {

    @Autowired
    MovieService movieService;

    @PostMapping("/get-list-page")
    @ApiOperation("条件查询所有电影带分页")
    public Result getListPage(@RequestBody MovieQueryVO queryVO){
        IPage<MovieQueryResponseDTO> movies = movieService.getListPage(queryVO);
        return Result.success(movies);
    }

    @GetMapping("/get-list-json-by-keyword")
    @ApiOperation("根据关键词模糊查询电影集合")
    public Result getListJsonByKeyword(@RequestParam(value = "keyword",required = false) String keyWord){
        List<Movie> movies = movieService.getListJsonByKeyword(keyWord);
        return Result.success(movies);
    }

    @GetMapping("/get-by-id")
    @NeedPermissions("movie:select")
    @ApiOperation("通过id查询电影")
    public Result getById(@ApiParam(value = "电影id",required = true)Long id){
        return Result.success(movieService.findById(id));
    }

    @PostMapping("/save")
    @NeedPermissions("movie:save")
    @ApiOperation("新增电影")
    public Result save(@RequestBody MovieSaveVO movieSaveVO){
        Movie movie = new Movie();
        BeanUtils.copyProperties(movieSaveVO,movie);
        movieService.add(movie);
        return Result.success(null,"电影添加成功！！");
    }

    @GetMapping("/delete-one")
    @NeedPermissions("movie:delete")
    @ApiOperation("删除电影")
    public Result deleteOne(@ApiParam(value = "电影id",required = true)Long id){
        try {
            movieService.removeById(id);
        } catch (Exception e) {
            e.printStackTrace();
            throw new AppException(ResponseEnum.FAILED);
        }
        return Result.success(null,"电影删除成功！！");
    }

    @GetMapping("/delete")
    @NeedPermissions("movie:delete")
    @ApiOperation("删除多个电影")
    public Result deleteOne(@ApiParam(value = "电影id数组",required = true)Long[] ids){
        List<Long> idList = Arrays.stream(ids).collect(Collectors.toList());
        try {
            movieService.removeByIds(idList);
        } catch (Exception e) {
            e.printStackTrace();
            throw new AppException(ResponseEnum.FAILED);
        }
        return Result.success(null,"电影删除成功！！");
    }

    @PostMapping("/update")
    @NeedPermissions("movie:update")
    @ApiOperation("修改电影")
    public Result update(@RequestBody MovieUpdateVO movieUpdateVO) {
        Movie movie = new Movie();
        BeanUtils.copyProperties(movieUpdateVO,movie);
        movieService.updateMovie(movie);
        return Result.success(null,"电影修改成功！！");
    }
}
